Telling your computer what to do

Telling your computer what to do

Some programming languages you might have heard of:

  • R
  • Matlab
  • Python
  • JavaScript
  • html

Interacting with your computer

Interacting with your computer

Interacting with your computer

Creating variables

baby <- 'Meredith'
sculpture <- 10
baby 
## [1] "Meredith"
sculpture
## [1] 10

What types of information does your computer understand?

  • numeric
    • integers, e.g. 1, 2, 3
    • doubles, e.g. 1.25, 3.78
    • complex numbers, e.g. 3 + 2i
  • character or string
    • e.g. "Research", "Platforms"
  • logical or Boolean
    • e.g. TRUE and FALSE

How can we group information?

  • vectors / tuples
  • matrices / arrays
  • lists / structures / data frames
  • dictionaries

vectors / tuples

vectors / tuples

vector1 <- 1:10
vector2 <- c("learning", "to", "code", "is", "fun")
vector1
##  [1]  1  2  3  4  5  6  7  8  9 10
vector2
## [1] "learning" "to"       "code"     "is"       "fun"

matrices / arrays

matrices / arrays

PlatoonLeads <- matrix(
  c("Data Wranglers", 
    "Data Miners",  
    "Cadventurers", 
    "Kerry Halupka", 
    "Kim Doyle",  
    "Louise van der Werff"), 
  ncol=2,
  dimnames=list(NULL,c("platoon","name"))) 
PlatoonLeads
##      platoon          name                  
## [1,] "Data Wranglers" "Kerry Halupka"       
## [2,] "Data Miners"    "Kim Doyle"           
## [3,] "Cadventurers"   "Louise van der Werff"

lists / structures / data frames

lists / structures / data frames

shoppingList <- list(
  lunch         = c("bread", "cheese", "tomato"),
  dinner        = c("frozen pizza", "chocolate mousse"),
  moneyInWallet = 40.50
)
shoppingList
## $lunch
## [1] "bread"  "cheese" "tomato"
## 
## $dinner
## [1] "frozen pizza"     "chocolate mousse"
## 
## $moneyInWallet
## [1] 40.5

dictionaries

dictionaries

IMDBRatings = {"Game of Thrones": 9.4, 
    "Sherlock": 9.2, 
    "Firefly": 9.1, 
    "Friends": 8.9}

for movie, rating in IMDBRatings.items():
    print movie + ': ' + str(rating)
## Firefly: 9.1
## Friends: 8.9
## Sherlock: 9.2
## Game of Thrones: 9.4

How to add flexibility to the conversation

If statements

How to add flexibility to the conversation

If statements

hungry <- TRUE
if (hungry){
  print("GIVE ME A TIM TAM!!!")
} else {
  print("No tim tam for me, thanks!")
}
## [1] "GIVE ME A TIM TAM!!!"

How to add flexibility to the conversation

If statements

hungry <- FALSE
if (hungry){
  print("GIVE ME A TIM TAM!!!")
} else {
  print("No tim tam for me, thanks!")
}
## [1] "No tim tam for me, thanks!"

How to add flexibility to the conversation

For loops

How to add flexibility to the conversation

For loops

for (beats in 1:4){
  print("bring left foot to right foot")
  print("step left foot back to original position")
  print("bring right foot to left foot")
  print("step left foot back to original position")
}
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"
## [1] "bring left foot to right foot"
## [1] "step left foot back to original position"
## [1] "bring right foot to left foot"
## [1] "step left foot back to original position"

How to add flexibility to the conversation

Functions

How to add flexibility to the conversation

Functions

moodCalendar <- function(day){
  if (day %in% c("Friday", "Saturday", "Sunday")){
    print(paste(day, "is a happy day"))
  } else if (day %in% c("Monday", "Tuesday")){
    print(paste(day, "is a grumpy day"))
  } else {
    print(paste(day, "is a neutral day"))
  }}
moodCalendar("Monday")
## [1] "Monday is a grumpy day"

How to add flexibility to the conversation

Functions

moodCalendar <- function(day){
  if (day %in% c("Friday", "Saturday", "Sunday")){
    print(paste(day, "is a happy day"))
  } else if (day %in% c("Monday", "Tuesday")){
    print(paste(day, "is a grumpy day"))
  } else {
    print(paste(day, "is a neutral day"))
  }}
moodCalendar("Friday")
## [1] "Friday is a happy day"

Where to find help

Making mistakes is the best way to learn

Where to learn more